<?php

/**
 * 
 * @author yuxun
 *
 */
class InsidePlayerController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('内玩元宝申请');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$special_id = $this->R('special_id_s');
		$server_id = $this->R('server_id');
		
		$filter->addSpecial_s(true);
		$filter->addServerFilterALL(true,$special_id,$server_id);
		$filter->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
		
		$statusList = InsidePlayerService::status();
		$statusList = array('' => '所有') + $statusList;
		$filter->addFilter('status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
	
		$params = $filter->get();
		$params['special_id'] = $params['special_id_s'];
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = InsidePlayerService::indexHeader();
		if(PubFunc::isAjax()){
			$result = InsidePlayerService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=insidePlayer/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'insidePlayer/index',
					'length'=>10,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
		}
	}
	
	
	public function approveList(){
		$this->setTitle('内玩元宝审批列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$special_id = $this->R('special_id_s');
		$server_id = $this->R('server_id');
	
		$filter->addSpecial_s(true);
		$filter->addServerFilterALL(true,$special_id,$server_id);
		$filter->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
	
		$statusList = InsidePlayerService::status();
		//$statusList = array('' => '所有') + $statusList;
		$filter->addFilter('status', 'select', '状态', array(
				'options'   => $statusList,
				'value'     => key($statusList)
		));
	
		$params = $filter->get();
		$params['special_id'] = $params['special_id_s'];
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = InsidePlayerService::indexHeader();
		if(PubFunc::isAjax()){
			$result = InsidePlayerService::approveData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=insidePlayer/approveList", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'insidePlayer/approveList',
					'length'=>10,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
		}
	}
	
	public function insideList(){
		$this->setTitle('内玩号管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$special_id = $this->R('special_id_s');
		$server_id = $this->R('server_id');
	
		$filter->addSpecial_s(true);
		$filter->addServerFilterALL(true,$special_id,$server_id);
		$filter->addFilter('role_name', 'input', '角色名');
		$filter->addFilter('role_id', 'input', '角色ID');
		$filter->addExport();
	
		$params = $filter->get();
		$params['special_id'] = $params['special_id_s'];
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = InsidePlayerService::insideHeader();
	    if($filter->isExport()){
	    	unset($header['action']);
            ini_set('memory_limit','1024M');
            $data   = InsidePlayerService::insideData($params,true);
            LibExcel::exportCsv($header, $data, "Mango数据后台-内玩玩家列表({$params['time']}).csv");
            exit();
        }else if(PubFunc::isAjax()){
			$result = InsidePlayerService::insideData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=insidePlayer/insideList", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'insidePlayer/insideList',
					'length'=>10,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
		}
	}
	


    public function add(){
        $this->setTitle('元宝申请');
        $this->out['specialList'] =SpecialServerService::specialList();       
    }

    public function doAdd(){
        $result = InsidePlayerService::doAdd($_POST);

        if($result !== false){
            $this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=insidePlayer/index"));
        }else{
            $this->showMessage('新增失败');
        }
    }


    public function delete(){
        $id = $this->R('id');

        $result = InsidePlayerService::delete($id);

        if($result !== false){
            $this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=insidePlayer/index"));
        }else{
            $this->showMessage('删除失败');
        }
    }
    
    /**
     * 批准、驳回
     */
    public function setStatus(){
    	$id = $this->R('id');
    	$status = $this->R('status');
    
    	$result = InsidePlayerService::setStatus($id,$status);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 查看审批信息
     */
    public function getApproval(){
    	$this->layout = '';
    	$id = PubRequest::R('id');
    
    	$result = InsidePlayerService::getApproval($id);
    	if(!$result){
    		$result = array();
    	}
    
    	$this->out['list']      = $result;
    }
    
    /**
     * 检查角色是否存在
     */
    public function checkRole(){
    	$roleId = PubRequest::R('roleId');
    	$server = PubRequest::R('server');
    	$type = PubRequest::R('type');
    
    	$params = array(
    			'uid'    => $roleId,
    			'search_type'    => $type,
    	);
    	$api = new ApiService($server);
    	$result = $api->roleInfo($params);
    	
    	if($result['state']){
    		$msg = '该角色存在';
    		$status = 1;
    	}else{
    		$msg = '该角色不存在';
    		$status = 0;
    	}
    	
    	$data = array('status'=>$status,'msg'=>$msg,'data'=>'');
    
    	$this->out = $data;
    	$this->outType = 'json';
    }
    
    public function banUser(){
    	$id = PubRequest::R('id');
    	$actiontype = PubRequest::R('actiontype');
    	$result = InsidePlayerService::banUser($id,$actiontype);
    	$this->out = $result;
    	$this->outType = 'json';
    }

    
    

}